<?php
class RoleController extends CAdmin {
	private $role_db,$priv_db;
	function __construct(){
		parent::__construct();
		$this->role_db = new Admin_role_Model();
		$this->priv_db = new Admin_role_priv_Model();
	}
	
	public function init(){
	 $infos = $this->role_db->select();
	 include self::admin_tpl('role_list');
	}
	
	public function add(){
		if(isset($_POST['dosubmit'])){
			$info = $this->checkinfo($_POST['info']);
			$insert_id = $this->role_db->insert($_POST['info'],true);
			$this->_cache();
			if($insert_id){
				self::showmessage('操作成功！','?m=admin&c=role');
			}
		} else {
			include self::admin_tpl('role_add');
		}
	}
	
	public function edit(){
		if(isset($_POST['dosubmit'])){
			$roleid = intval($_POST['roleid']);
			if(!is_array($_POST['info']) || empty($_POST['info']['rolename'])){
				self::showmessage('操作失败'); return false;
			}
			$this->role_db->update($_POST['info'],array('roleid'=>$roleid));
			$this->_cache();
			self::showmessage('操作成功！','?m=admin&c=role');
		} else {
			$roleid = intval($_GET['roleid']);
			$info = $this->role_db->get_one(array('roleid'=>$roleid));
			extract($info);
			include self::admin_tpl('role_edit');		
		}
	}
	
	public function delete(){
		$roleid = intval($_GET['roleid']);
		if($roleid == '1') self::showmessage('不能删除角色',HTTP_REFERER);
		$this->role_db->delete(array('roleid'=>$roleid));
		$this->priv_db->delete(array('roleid'=>$roleid));
		$this->_cache();
		self::showmessage('操作成功！','?m=admin&c=role');
	}
	
	/*
	 * 权限设置
	 */
	public function role_priv(){		
		if(isset($_POST['dosubmit'])){
			$roleid = intval($_POST['roleid']);			
			$rolename = $_POST['rolename'];
			$menuids = isset($_POST['menuid']) ? $_POST['menuid'] : '';
			if (is_array($menuids) && count($menuids) > 0) {
				
				$this->priv_db->delete(array('roleid'=>$roleid));
				foreach ($menuids as $menuid) {
					$info = array();
					$info = $this->get_menuinfo(intval($menuid));
					$info['roleid'] = $roleid;
					$this->priv_db->insert($info);
				}
			} else {
				$this->priv_db->delete(array('roleid'=>$roleid));
			}
			self::showmessage('操作成功！', '?m=admin&c=role');
		} else{
			$menu_db = new Menu_Model();
			$roleid = intval($_GET['roleid']);			
			$rolename = $_GET['rolename'];
			$menu = new CTree();
			$menu->icon = array('│  ','├─  ','└─  ');
			$menu->nbsp = '&nbsp;&nbsp;&nbsp;';
			$result = $menu_db->select();
			$priv = $this->priv_db->select(array('roleid'=>$_GET['roleid']));
			foreach ($result as $key => $value) {
				$result[$key]['checked'] = ($this->is_checked($value, $priv, $roleid)) ? ' checked' : '';
				$result[$key]['level'] = $this->get_level($value['menu_id'],$result);
				$result[$key]['parentid_node'] = $value['parentid'] ? 'class="child-of-node-'.$value['parentid'].'"' : '';
			}
			$str = "<tr id='node-\$id' \$parentid_node>
						<td >\$spacer <input type='checkbox' name='menuid[]' value='\$menu_id' level='\$level' \$checked onclick='javascript:checknode(this);'>\$name</td>
					</tr>";
			$menu->init($result);
			$categorys = $menu->get_tree(0, $str);
		}
		include self::admin_tpl('role_priv');
	}
	
	/*
	 * 成员管理
	 */
	public function member_manage(){
		$roleid = intval($_GET['roleid']);
		$admin_db = new Admin_Model();
		$infos = $admin_db->select(array('roleid'=>$roleid),"userid,username,roleid,lastloginip,lastlogintime,email,realname");
		$cache = new Caches();
		$roles = $cache->getcache('role','commons');
		include self::admin_tpl('admin_list');
	}
	

	/**
	 * 缓存
	 * Enter description here ...
	 */
	private function _cache(){
		$infos = $this->role_db->select();
		$role = array();
		foreach ($infos as $info) {
			$role[$info['roleid']] = $info['rolename'];
		}
		$cache = new Caches();
		$cache->stecache('role', $role, 'commons');
		return $infos;
	}
	
	/**
	 * 检测数据
	 * Enter description here ...
	 */
	private function checkinfo($info){
		if(!is_array($info) || empty($info['rolename'])){
			self::showmessage('操作失败'); return false;
		}
		if($this->checkname($info['rolename'])){
			self::showmessage('角色已存在！'); return false;
		}
		return $info;
	}
	/**
	 * 检测rolename是否重复
	 * Enter description here ...
	 * @param unknown_type $name
	 */
	private function checkname($name){
		$info = $this->role_db->get_one(array('rolename'=>$name),'roleid');
		if($info['roleid']){
			return true;
		}
		return false;
	}
	
	/**
	 * 异步检测name
	 */
	public function public_checkname_ajx(){
		$rolename = isset($_GET['rolename']) ? trim($_GET['rolename']) : exit(0);
		if ($this->role_db->get_one(array('rolename'=>$rolename),'roleid')){
			exit('0');
		} 
		exit('1');
	}
	
	/**
	 * 检查菜单是否有权限
	 * Enter description here ...
	 * @param unknown_type $menu  要检查的菜单
	 * @param unknown_type $priv  这个角色的权限
	 * @param unknown_type $roleid 要检查的id
	 */
	private function is_checked($menu, $priv, $roleid){
		$priv_arr = array('m','c','a');
		if($menu['m'] == '') return false;
		foreach ($menu as $key => $value) {
			if(!in_array($key, $priv_arr)) unset($menu[$key]);
		}
		$menu['roleid'] = $roleid;
		if(in_array($menu, $priv)){
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 菜单深度
	 * Enter description here ...
	 * @param unknown_type $id
	 * @param unknown_type $menu
	 * @param unknown_type $i
	 */
	private function get_level($id, $menu = array(),$i = 0){
		foreach ($menu as $key => $value) {
			if($value['menu_id'] == $id){
				if($value['parentid'] == 0) return $i;
				$i++;
				return $this->get_level($value['parentid'], $menu, $i);
			}
		}
	}
	
	/**
	 * 获得菜单信息
	 */
	private function get_menuinfo($menuid){
		$menudb = new Menu_Model();
		$menuinfos = $menudb->select('','menu_id,m,c,a');
		foreach ($menuinfos as $menuinfo) {
			$menuinfos[$menuinfo['menu_id']] = $menuinfo;
		}
		$menuid = intval($menuid);
		unset($menuinfos[$menuid]['menu_id']);
		return $menuinfos[$menuid];
	}
}